﻿<vi:PageView x:Class="RapidDownloader.Modules.Views.DownloadModule.Views.Download"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:markup="clr-namespace:RapidDownloader.Modules.Views.Markup"
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
             Title="Download" xmlns:vi="http://schemas.rhea.be/vidyano/2007/presentation">

    <vi:PageView.Resources>
        <markup:FormatSizeConverter x:Key="formatSize" />
    </vi:PageView.Resources>

    <vi:Container>
        <vi:Container.Template>
            <ControlTemplate>
                <ContentPresenter Content="{Binding Items[0], RelativeSource={RelativeSource TemplatedParent}}" />
            </ControlTemplate>
        </vi:Container.Template>

        <ListView ItemsSource="{Binding Module.Context.Files}" TextElement.Foreground="Black">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="VerticalContentAlignment" Value="Stretch" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Image Width="16" Height="16" RenderOptions.BitmapScalingMode="HighQuality" Source="{Binding ProviderIcon}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Download State" Width="100">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Visibility="Visible" vi:ViewModel.Scope="StartOrResume">
                                            <Button.Template>
                                                <ControlTemplate TargetType="Button">
                                                    <Border Background="Transparent" Cursor="Hand">
                                                        <TextBlock Text="{Binding DownloadState}" x:Name="txt" />
                                                    </Border>

                                                    <ControlTemplate.Triggers>
                                                        <DataTrigger Binding="{Binding DownloadState}" Value="Retrying">
                                                            <Setter Property="Text" TargetName="txt">
                                                                <Setter.Value>
                                                                    <MultiBinding StringFormat="{}{0} ({1}/{2})">
                                                                        <Binding Path="DownloadState" />
                                                                        <Binding Path="RetryCount" />
                                                                        <Binding Path="MaxRetryCount" />
                                                                    </MultiBinding>
                                                                </Setter.Value>
                                                            </Setter>
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding DownloadState}" Value="Failed">
                                                            <Setter Property="Foreground" Value="Red" TargetName="txt" />
                                                            <Setter Property="Text" Value="{Binding DownloadState, StringFormat='(Retry) {0}'}" TargetName="txt" />
                                                        </DataTrigger>
                                                    </ControlTemplate.Triggers>
                                                </ControlTemplate>
                                            </Button.Template>
                                        </Button>
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="File Name">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding FileName}" ToolTip="{Binding Link}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Download Progress" Width="200">
                            <GridViewColumn.DisplayMemberBinding>
                                <MultiBinding StringFormat="{}{0}/{1} ({2:0.00}%)">
                                    <Binding Path="TotalBytes" Converter="{StaticResource formatSize}" />
                                    <Binding Path="Size" Converter="{StaticResource formatSize}" />
                                    <Binding Path="DownloadProgress" />
                                </MultiBinding>
                            </GridViewColumn.DisplayMemberBinding>
                        </GridViewColumn>
                        <GridViewColumn Header="Speed" Width="90">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="-" x:Name="txt" />
                                    <DataTemplate.Triggers>
                                        <DataTrigger Binding="{Binding DownloadState}" Value="InProgress">
                                            <Setter Property="Text" Value="{Binding BandwidthUsage, Converter={StaticResource formatSize}, StringFormat='{}{0}/s'}" TargetName="txt" />
                                        </DataTrigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Time Remaining" DisplayMemberBinding="{Binding TimeRemaining}" Width="80" />
                        <GridViewColumn Width="120">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ProgressBar Minimum="0" Maximum="100" Value="{Binding DownloadProgress, Mode=OneWay}">
                                        <ProgressBar.Style>
                                            <Style TargetType="ProgressBar" BasedOn="{StaticResource {x:Type ProgressBar}}">
                                                <Setter Property="Foreground">
                                                    <Setter.Value>
                                                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                                            <GradientStop Color="Lime"/>
                                                            <GradientStop Color="#FF007F00" Offset="1.0"/>
                                                        </LinearGradientBrush>
                                                    </Setter.Value>
                                                </Setter>
                                                <Setter Property="BorderBrush" Value="DarkGreen" />
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding DownloadState}" Value="Failed">
                                                        <Setter Property="Foreground">
                                                            <Setter.Value>
                                                                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                                                    <GradientStop Color="Red"/>
                                                                    <GradientStop Color="#FF7F0000" Offset="1"/>
                                                                </LinearGradientBrush>
                                                            </Setter.Value>
                                                        </Setter>
                                                        <Setter Property="BorderBrush" Value="DarkRed" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </ProgressBar.Style>
                                    </ProgressBar>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="{Binding FailReason}" Width="250" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
    </vi:Container>
</vi:PageView>